#include "register_dialog.h"
#include "ui_register_dialog.h"
#include "QPixmap"
#include "account.h"
register_dialog::register_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::register_dialog)
{
    ui->setupUi(this);
    this->setWindowTitle("注册新用户");

    //设置星号提示信息
    QPixmap pix1(":/asterisk.png");
    pix1 = pix1.scaled(50,50);
    ui->pushButton->setIcon(pix1);
    ui->pushButton->setToolTip("用户名不能为空");
    ui->pushButton_2->setIcon(pix1);
    ui->pushButton_2->setToolTip("密码不能为空");
    ui->pushButton_3->setIcon(pix1);
    ui->pushButton_3->setToolTip("密码不能为空");
    ui->pushButton_4->setIcon(pix1);
    ui->pushButton_4->setToolTip("手机号码不能为空");
    //设置查看密码按钮
    QPixmap pix2(":/eyes_check.jpg");
    pix2 = pix2.scaled(25,25);
    ui->check_password->setIcon(pix2);
    ui->check_repassword->setIcon(pix2);
    //初始化对钩与叉号
    right = QPixmap(":/right.png");
    mistake = QPixmap(":/mistake.png");
    right = right.scaled(25,25);
    mistake = mistake.scaled(25,25);
    //初始化状态与提交按钮
    ui->status->setPixmap(mistake);
    ui->submit->setEnabled(false);
}

register_dialog::~register_dialog()
{
    delete ui;
}

void register_dialog::on_user_name_textChanged(const QString &arg1)
{
    (void)arg1;
    setIfLegal();
}

void register_dialog::on_password_textChanged(const QString &arg1)
{
    (void)arg1;
    setIfLegal();
}

void register_dialog::on_repassword_textChanged(const QString &arg1)
{
    (void)arg1;
    setIfLegal();
}

void register_dialog::on_tel_number_textChanged(const QString &arg1)
{
    (void)arg1;
    setIfLegal();
}

bool register_dialog::checkPassword()
{
    QString s1 = ui->password->text();
    QString s2 = ui->repassword->text();
    if(s1.isEmpty() || s2.isEmpty())
        return false;
    else
    {
        if(s1 == s2) return true;
        else return false;
    }
    return false;
}

void register_dialog::setIfLegal()
{
    if(!ui->user_name->text().isEmpty() && checkPassword()
            &&!ui->tel_number->text().isEmpty())
    {
        ui->status->setPixmap(right);
        ui->submit->setEnabled(true);
    }
    else
    {
        ui->status->setPixmap(mistake);
        ui->submit->setEnabled(false);
    }
}

void register_dialog::on_check_password_clicked()
{
    if(ui->password->echoMode() == QLineEdit::Password)
        ui->password->setEchoMode(QLineEdit::Normal);
    else
        ui->password->setEchoMode(QLineEdit::Password);
}

void register_dialog::on_check_repassword_clicked()
{
    if(ui->repassword->echoMode() == QLineEdit::Password)
        ui->repassword->setEchoMode(QLineEdit::Normal);
    else
        ui->repassword->setEchoMode(QLineEdit::Password);
}

void register_dialog::on_submit_clicked()
{
    QString name = ui->user_name->text();
    QString password = ui->password->text();
    QString telnumber = ui->tel_number->text();
    account acc = account(0,name,password,telnumber);
    acc.insertToDB();
}
